﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using sauyou.Utils;
using System.Data.SqlClient;

namespace sauyou.Admin
{
    public partial class User_Add : ManagePage
    {
        /// <summary>
        /// 编号
        /// </summary>
        private string mDataId
        {
            get
            {
                object obj = (object)ViewState["mdataId"];
                if (obj != null)
                    return (string)obj;
                else
                    return "";
            }
            set { ViewState["mdataId"] = value; }
        }

        public sauyou.DAL.Article bll = new sauyou.DAL.Article();
        public sauyou.Model.Article model = new sauyou.Model.Article();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                mDataId = Request["dataId"];
                BindDLL();
                if (!string.IsNullOrEmpty(mDataId))
                {
                    chkLoginLevel("editMember");
                    ShowInfo();
                }
                else
                {
                    chkLoginLevel("addMember");
                    UpdatepwdTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    registerTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    endTime.Text = Convert.ToDateTime(registerTime.Text).AddDays(5).ToString("yyyy-MM-dd HH:mm:ss");
                    oldTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    //oldIP.Text=

                }

            }
        }

        private void BindDLL()
        {
            //用户类型
            string sqlyhlx = "SELECT * FROM user_yhlx";
            DataTable dtyhlx = DbHelperSQL.Query(sqlyhlx).Tables[0];
            yhlxid.DataSource = dtyhlx.DefaultView;
            yhlxid.DataTextField = "name";
            yhlxid.DataValueField = "ID";
            yhlxid.DataBind();


            //用户代理等级
            string sqldldj = "SELECT * FROM user_dldj";
            DataTable dt = DbHelperSQL.Query(sqlyhlx).Tables[0];
            dldjid.DataSource = dt.DefaultView;
            dldjid.DataTextField = "name";
            dldjid.DataValueField = "ID";
            dldjid.DataBind();

        }

        //赋值操作
        private void ShowInfo()
        {
            string sql = "select * from member";
        }

        private bool SaveData()
        {

            string sql = "";
            SqlParameter[] parameters = {
					new SqlParameter("@userName", SqlDbType.NChar,10),
					new SqlParameter("@memberpwd", SqlDbType.NVarChar,100),
					new SqlParameter("@phone", SqlDbType.NVarChar,100),
					new SqlParameter("@readName", SqlDbType.NVarChar,100),
					new SqlParameter("@emial", SqlDbType.NVarChar,100),
					new SqlParameter("@totalMoney", SqlDbType.Decimal,9),
					new SqlParameter("@yhlxid", SqlDbType.Int,4),
					new SqlParameter("@qq", SqlDbType.NVarChar,50),
					new SqlParameter("@address", SqlDbType.NVarChar,250),
					new SqlParameter("@zipCode", SqlDbType.NVarChar,50),
					new SqlParameter("@endTime", SqlDbType.NVarChar,50),				
					new SqlParameter("@tips", SqlDbType.NVarChar,200),
					new SqlParameter("@dldjid", SqlDbType.Int,4),
					new SqlParameter("@parentId", SqlDbType.Int,4),
					new SqlParameter("@oldTime", SqlDbType.DateTime),
					new SqlParameter("@oldIP", SqlDbType.NVarChar,100),
					new SqlParameter("@registerIP", SqlDbType.NVarChar,100),
					new SqlParameter("@registerTime", SqlDbType.DateTime),
					new SqlParameter("@state", SqlDbType.Int,4)};

            parameters[0].Value = userName.Text.Trim();
            parameters[1].Value = DESEncrypt.Encrypt(memberpwd.Text.Trim());
            parameters[2].Value = phone.Text.Trim();
            parameters[3].Value = readName.Text.Trim();
            parameters[4].Value = emial.Text.Trim();
            parameters[5].Value = 0;//总花费
            parameters[6].Value = Convert.ToInt32(yhlxid.SelectedValue);
            parameters[7].Value = qq.Text.Trim();
            parameters[8].Value = address.Text.Trim();
            parameters[9].Value = zipCode.Text.Trim();
            parameters[10].Value = endTime.Text.Trim();
           
            //parameters[12].Value = model.tips;
            //parameters[13].Value = dldjid.SelectedValue;
            //parameters[14].Value = model.parentId;
            //parameters[15].Value = oldTime.Text.Trim();
            //parameters[16].Value = model.oldIP;
            //parameters[17].Value = model.registerIP;
            //parameters[18].Value = model.registerTime;
            //parameters[19].Value = model.state;
            if (!string.IsNullOrEmpty(mDataId))
            {
                sql = @"";
                //保存日志
                SaveLogs("[会员模块]编辑会员：" + model.Title);
                
            }
            else
            {
                sql = @"insert into Member(userName,memberpwd,phone,readName,emial,totalMoney,yhlxid,qq,address,zipCode,endTime,
                        tips,dldjid,parentId,oldTime,oldIP,registerIP,registerTime,state) Values(
                        @userName,@memberpwd,@phone,@readName,@emial,@totalMoney,@yhlxid,@qq,@address,@zipCode,@endTime,
                        @tips,@dldjid,@parentId,@oldTime,@oldIP,@registerIP,@registerTime,@state);select @@IDENTITY";

                object obj = DbHelperSQL.GetSingle(sql, parameters);
                if (obj == null)
                {
                    return false;
                }
                else
                {
                    mDataId = Convert.ToString(obj);
                    return true;
                }

                //保存日志
                SaveLogs("[会员模块]添加会员：" + readName.Text);               
            }
            return true;
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!SaveData())
            {
                JscriptPrint("添加会员成功啦！", "User_List.aspx", "Success");
            }
            else {
                JscriptPrint("添加会员失败啦！", "User_List.aspx", "Error");
            }

        }


    }
}
